package com.goldsprite.ruletilemap.examples;

import com.goldsprite.ruletilemap.core.RuleTile;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RuleConfig {

    public static RuleTile createGrassRuleTile() {
        List<RuleTile.RuleEntry> ruleEntries = new ArrayList<>();

        // 基础规则
        {
            // 左上角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "1", "" +
                    "???" +
                    "?oy" +
                    "?yy"));
            // 上中
            ruleEntries.add(new RuleTile.RuleEntry(
                    "2", "" +
                    "???" +
                    "yoy" +
                    "yyy"));
            // 右上角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "3", "" +
                    "???" +
                    "yo?" +
                    "yy?"));
            // 左中
            ruleEntries.add(new RuleTile.RuleEntry(
                    "4", "" +
                    "?yy" +
                    "?oy" +
                    "?yy"));
            // 中心
            ruleEntries.add(new RuleTile.RuleEntry(
                    "5", "" +
                    "yyy" +
                    "yoy" +
                    "yyy"));
            // 中心
            ruleEntries.add(new RuleTile.RuleEntry(
                    "10", "" +
                    "yyy" +
                    "yoy" +
                    "yyy"));
            // 右中
            ruleEntries.add(new RuleTile.RuleEntry(
                    "6", "" +
                    "yy?" +
                    "yo?" +
                    "yy?"));
            // 左下角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "7", "" +
                    "?yy" +
                    "?oy" +
                    "???"));
            // 下中
            ruleEntries.add(new RuleTile.RuleEntry(
                    "8", "" +
                    "yyy" +
                    "yoy" +
                    "???"));
            // 右下角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "9", "" +
                    "yy?" +
                    "yo?" +
                    "???"));
        }

        // 高级规则(拐角)
        {
            // 左上拐角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "9", "" +
                    "nyy" +
                    "yoy" +
                    "yyy"));
            // 右上拐角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "7", "" +
                    "yyn" +
                    "yoy" +
                    "yyy"));
            // 左下拐角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "3", "" +
                    "yyy" +
                    "yoy" +
                    "nyy"));
            // 右下拐角
            ruleEntries.add(new RuleTile.RuleEntry(
                    "1", "" +
                    "yyy" +
                    "yoy" +
                    "yyn"));
        }

        String attentionList = "g1 1 2 3 4 5 6 7 8 9";
        return new RuleTile("g1", ruleEntries, attentionList);
    }

    public static String createRandomMap(int width, int height, double grassDensity) {
        Random random = new Random();
        StringBuilder map = new StringBuilder();

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                // 边界总是空气
                if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
                    map.append("0");
                } else {
                    // 根据密度随机生成草地或空气
                    if (random.nextDouble() < grassDensity) {
                        map.append("g1");
                    } else {
                        map.append("0");
                    }
                }

                if (x < width - 1) {
                    map.append(" ");
                }
            }
            if (y < height - 1) {
                map.append("\n");
            }
        }

        return map.toString();
    }
}
